package cn.codenes.logconsumer.service.impl;

import cn.codenes.logconsumer.service.LogService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ：Hyman
 * @date ：Created in 2021/7/30 15:36
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class LogServiceImpl implements LogService {
    @Override
    @RabbitListener(queues = {"debugQueue"})
    public void writeDebug(Message message) {
        String str = new String(message.getBody());
        System.out.println(str);
    }

    @Override
    @RabbitListener(queues = {"infoQueue"})
    public void writeInfo(Message message) {
        String str = new String(message.getBody());
        System.out.println(str);
    }

    @Override
    @RabbitListener(queues = {"errorQueue"})
    public void writeError(Message message) {
        String str = new String(message.getBody());
        System.out.println(str);
    }

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    @RabbitListener(queues = {"eventQueue"})
    public void getMsg(Message message) {
        String str = new String(message.getBody());
        System.out.println(str);
        String replayTo = message.getMessageProperties().getReplyTo();
        System.out.println("replayTo =" + replayTo);
        byte[] msgBytes = "我收到了".getBytes();
        MessageProperties messageProperties = new MessageProperties();
        Message replayMessage = new Message(msgBytes, messageProperties);
        try {
            rabbitTemplate.send(replayTo, replayMessage);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }
}
